package com.hao.syn;

import java.util.Arrays;

public class SafeTicket {
    public static void main(String[] args) {
        BTicket station = new BTicket();
        for (String s : Arrays.asList("世浩", "浩浩","黄牛")) {
            new Thread(station, s).start();
        }
    }
}
class BTicket implements Runnable{
    private int ticketNums =1000;
    boolean flag = true;
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    private synchronized void buy() throws InterruptedException {
        if (ticketNums<=0){
            flag = false;
            return;
        }

        System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);
    }
}
